package main

import (
	"fmt"
	"github.com/aws/aws-sdk-go/aws"
	"github.com/aws/aws-sdk-go/aws/credentials"
	"github.com/aws/aws-sdk-go/aws/session"
	"github.com/aws/aws-sdk-go/service/ec2"
)

func GetInstances() (*ec2.DescribeInstancesOutput, error) {
	sess, _ := session.NewSession(&aws.Config{
		Credentials: credentials.NewStaticCredentials("AKIASAM5FQYW6TS2KGM3", "2kmQQ3dnQv7/il9Q+R/VRv5bwb1VSyjiG7OByW69", ""),
		Region:      aws.String("ap-northeast-1"),
	})
	svc := ec2.New(sess)
	result, err := svc.DescribeInstances(nil)

	if err != nil {
		return nil, err
	}
	return result, nil
}

func main() {
	result, err := GetInstances()
	if err != nil {
		fmt.Println("Got an error retrieving information about your Amazon EC2 instances:")
		fmt.Println(err)
		return
	}
	for _, r := range result.Reservations {
		//fmt.Println(r)
		for _, i := range r.Instances {
			fmt.Println("   " + *i.InstanceId +
				"   " + *i.Tags[0].Value +
				"   " + *i.PrivateIpAddress +
				"   " + *i.State.Name +
				"   " + *i.InstanceType +
				"	" + i.LaunchTime.String())
		}
	}
}
